package cn.iocoder.yudao.module.erp.controller.admin.business.order.orderunit;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderunit.vo.OrderUnitPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderunit.vo.OrderUnitRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderunit.vo.OrderUnitSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderunit.OrderUnitDO;
import cn.iocoder.yudao.module.erp.service.business.order.orderunit.OrderUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 单位")
@RestController
@RequestMapping("/erp/order-unit")
@Validated
public class OrderUnitController {

    @Resource
    private OrderUnitService orderUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建单位")
    @PreAuthorize("@ss.hasPermission('erp:order-unit:create')")
    public CommonResult<Long> createOrderUnit(@Valid @RequestBody OrderUnitSaveReqVO createReqVO) {
        return success(orderUnitService.createOrderUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单位")
    @PreAuthorize("@ss.hasPermission('erp:order-unit:update')")
    public CommonResult<Boolean> updateOrderUnit(@Valid @RequestBody OrderUnitSaveReqVO updateReqVO) {
        orderUnitService.updateOrderUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:order-unit:delete')")
    public CommonResult<Boolean> deleteOrderUnit(@RequestParam("id") Long id) {
        orderUnitService.deleteOrderUnit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:order-unit:query')")
    public CommonResult<OrderUnitRespVO> getOrderUnit(@RequestParam("id") Long id) {
        OrderUnitDO orderUnit = orderUnitService.getOrderUnit(id);
        return success(BeanUtils.toBean(orderUnit, OrderUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单位分页")
    @PreAuthorize("@ss.hasPermission('erp:order-unit:query')")
    public CommonResult<PageResult<OrderUnitRespVO>> getOrderUnitPage(@Valid OrderUnitPageReqVO pageReqVO) {
        PageResult<OrderUnitDO> pageResult = orderUnitService.getOrderUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单位 Excel")
    @PreAuthorize("@ss.hasPermission('erp:order-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderUnitExcel(@Valid OrderUnitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderUnitDO> list = orderUnitService.getOrderUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单位.xls", "数据", OrderUnitRespVO.class,
                        BeanUtils.toBean(list, OrderUnitRespVO.class));
    }

}